{
  "nbformat": 4,
  "nbformat_minor": 5,
  "metadata": {},
  "cells": [
    {
      "metadata": {},
      "source": [
        "<td>\n",
        "   <a target=\"_blank\" href=\"https://labelbox.com\" ><img src=\"https://labelbox.com/blog/content/images/2021/02/logo-v4.svg\" width=256/></a>\n",
        "</td>"
      ],
      "cell_type": "markdown"
    },
    {
      "metadata": {},
      "source": [
        "<td>\n",
        "<a href=\"https://colab.research.google.com/github/Labelbox/labelbox-python/blob/master/examples/basics/projects.ipynb\" target=\"_blank\"><img\n",
        "src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"></a>\n",
        "</td>\n",
        "\n",
        "<td>\n",
        "<a href=\"https://github.com/Labelbox/labelbox-python/tree/master/examples/basics/projects.ipynb\" target=\"_blank\"><img\n",
        "src=\"https://img.shields.io/badge/GitHub-100000?logo=github&logoColor=white\" alt=\"GitHub\"></a>\n",
        "</td>"
      ],
      "cell_type": "markdown"
    },
    {
      "metadata": {},
      "source": [
        "# Projects"
      ],
      "cell_type": "markdown"
    },
    {
      "metadata": {},
      "source": [
        "* A project can be thought of as a specific labeling task on a set of labels\n",
        "* That set of labels is defined by the data rows attached to the project\n",
        "* Each project has an ontology which defines the types of annotations supported during the labeling process\n",
        "**Note that there is a lot of advanced usage that is not covered in this notebook. See examples/project_configuration/project_setup.ipynb for those functions**\n",
        "* Also note that deprecated functions are not explained here."
      ],
      "cell_type": "markdown"
    },
    {
      "metadata": {},
      "source": [
        "!pip install -q \"labelbox[data]\""
      ],
      "cell_type": "code",
      "outputs": [],
      "execution_count": null
    },
    {
      "metadata": {},
      "source": [
        "import labelbox as lb\n",
        "import labelbox.types as lb_types\n",
        "import uuid"
      ],
      "cell_type": "code",
      "outputs": [],
      "execution_count": null
    },
    {
      "metadata": {},
      "source": [
        "# API Key and Client\n",
        "Provide a valid api key below in order to properly connect to the Labelbox Client."
      ],
      "cell_type": "markdown"
    },
    {
      "metadata": {},
      "source": [
        "# Add your API key\n",
        "API_KEY = \"\"\n",
        "# To get your API key go to: Workspace settings -> API -> Create API Key\n",
        "client = lb.Client(api_key=API_KEY)"
      ],
      "cell_type": "code",
      "outputs": [],
      "execution_count": null
    },
    {
      "metadata": {},
      "source": [
        "### Create a project\n"
      ],
      "cell_type": "markdown"
    },
    {
      "metadata": {},
      "source": [
        "# Creates an empty project\n",
        "project = client.create_project(name=\"my-test-project\",\n",
        "                                description=\"a description\",\n",
        "                                media_type=lb.MediaType.Image)"
      ],
      "cell_type": "code",
      "outputs": [],
      "execution_count": null
    },
    {
      "metadata": {},
      "source": [
        "### Create a dataset with data rows"
      ],
      "cell_type": "markdown"
    },
    {
      "metadata": {},
      "source": [
        "dataset = client.create_dataset(name=\"project-demo-dataset\")\n",
        "global_keys = []\n",
        "uploads = []\n",
        "# Generate data rows\n",
        "for i in range(1,9):\n",
        "    gb_key = \"TEST-ID-%id\" % uuid.uuid1()\n",
        "    uploads.append({\n",
        "        'row_data':  f\"https://storage.googleapis.com/labelbox-datasets/People_Clothing_Segmentation/jpeg_images/IMAGES/img_000{i}.jpeg\",\n",
        "        \"global_key\": gb_key,\n",
        "    })\n",
        "    global_keys.append(gb_key)\n",
        "\n",
        "task = dataset.create_data_rows(uploads)\n",
        "task.wait_till_done()\n",
        "print(\"ERRORS: \" , task.errors)\n",
        "print(\"RESULT URL: \", task.result_url)"
      ],
      "cell_type": "code",
      "outputs": [],
      "execution_count": null
    },
    {
      "metadata": {},
      "source": [
        "### Add a data rows to a project \n"
      ],
      "cell_type": "markdown"
    },
    {
      "metadata": {},
      "source": [
        "project.create_batch(\n",
        "  \"project-demo\", # each batch in a project must have a unique name\n",
        "  global_keys=global_keys,  # paginated collection of data row objects, list of data row ids or global keys\n",
        "  priority=1 # priority between 1(highest) - 5(lowest)\n",
        ")"
      ],
      "cell_type": "code",
      "outputs": [],
      "execution_count": null
    },
    {
      "metadata": {},
      "source": [
        "### Create tags and assign them to a project\n",
        "In this section, we are creating a tag in the ontology and associating it with a project. Then we are listing the tags attached to a project.\n"
      ],
      "cell_type": "markdown"
    },
    {
      "metadata": {},
      "source": [
        "#### Create a tag"
      ],
      "cell_type": "markdown"
    },
    {
      "metadata": {},
      "source": [
        "# Get the organization\n",
        "organization = client.get_organization()\n",
        "\n",
        "tag = organization.create_resource_tag(\n",
        "  {\n",
        "    \"text\": \"new-tag-name\",\n",
        "    \"color\": \"4ed2f9\"\n",
        "  }\n",
        ")"
      ],
      "cell_type": "code",
      "outputs": [],
      "execution_count": null
    },
    {
      "metadata": {},
      "source": [
        "#### Assign the tag to a project"
      ],
      "cell_type": "markdown"
    },
    {
      "metadata": {},
      "source": [
        "tags = project.update_project_resource_tags([tag.uid])"
      ],
      "cell_type": "code",
      "outputs": [],
      "execution_count": null
    },
    {
      "metadata": {},
      "source": [
        "#### Get project tags"
      ],
      "cell_type": "markdown"
    },
    {
      "metadata": {},
      "source": [
        "tags = project.get_resource_tags()"
      ],
      "cell_type": "code",
      "outputs": [],
      "execution_count": null
    },
    {
      "metadata": {},
      "source": [
        "### Attach ontology and label data rows\n",
        "\n",
        "In this section, we are creating an ontology to attach to a project and creating labels to import as ground truths. We need this setup to demonstrate other methods later in the demo. For more information, please reference our [Ontology](https://docs.labelbox.com/reference/ontology) and [Import Image Annotation](https://docs.labelbox.com/reference/import-image-annotations) development guides."
      ],
      "cell_type": "raw"
    },
    {
      "metadata": {},
      "source": [],
      "cell_type": "markdown"
    },
    {
      "metadata": {},
      "source": [
        "Create your ontology"
      ],
      "cell_type": "raw"
    },
    {
      "metadata": {},
      "source": [
        "# Create normalized json with a radio classification\n",
        "ontology_builder = lb.OntologyBuilder(classifications=[  # List of Classification objects\n",
        "        lb.Classification(class_type=lb.Classification.Type.RADIO,\n",
        "                          name=\"radio_question\",\n",
        "                          options=[\n",
        "                              lb.Option(value=\"first_radio_answer\"),\n",
        "                              lb.Option(value=\"second_radio_answer\")\n",
        "                          ]),\n",
        "])\n",
        "# Creating an ontology\n",
        "ontology = client.create_ontology(\"test-ontology\",\n",
        "                                  ontology_builder.asdict())"
      ],
      "cell_type": "code",
      "outputs": [],
      "execution_count": null
    },
    {
      "metadata": {},
      "source": [
        "Attach ontology to project"
      ],
      "cell_type": "markdown"
    },
    {
      "metadata": {},
      "source": [
        "\n",
        "project.setup_editor(ontology)"
      ],
      "cell_type": "code",
      "outputs": [],
      "execution_count": null
    },
    {
      "metadata": {},
      "source": [
        "Create labels and upload them to project as ground truths"
      ],
      "cell_type": "markdown"
    },
    {
      "metadata": {},
      "source": [
        "# Create labels\n",
        "labels = []\n",
        "for global_key in global_keys:\n",
        "    labels.append(lb_types.Label(data=lb_types.ImageData(global_key=global_key),\n",
        "                   annotations=[\n",
        "                        # Create radio classification annotation for labels\n",
        "                        lb_types.ClassificationAnnotation(\n",
        "                         name=\"radio_question\",\n",
        "                         value=lb_types.Radio(answer=lb_types.ClassificationAnswer(\n",
        "                         name=\"second_radio_answer\")))\n",
        "                   ]))\n",
        "\n",
        "# Upload labels for the data rows in project\n",
        "upload_job = lb.LabelImport.create_from_objects(\n",
        "     client = client,\n",
        "     project_id = project.uid,\n",
        "     name=\"label_import_job\"+str(uuid.uuid4()),\n",
        "     labels=labels)\n",
        "\n",
        "upload_job.wait_until_done()\n",
        "\n",
        "print(f\"Errors: {upload_job.errors}\")"
      ],
      "cell_type": "code",
      "outputs": [],
      "execution_count": null
    },
    {
      "metadata": {},
      "source": [
        "### Move data rows in project to different task queues"
      ],
      "cell_type": "markdown"
    },
    {
      "metadata": {},
      "source": [
        "# Get list of task queues for project\n",
        "task_queues = project.task_queues()\n",
        "\n",
        "for task_queue in task_queues:\n",
        "    print(task_queue)"
      ],
      "cell_type": "code",
      "outputs": [],
      "execution_count": null
    },
    {
      "metadata": {},
      "source": [
        "project.move_data_rows_to_task_queue(data_row_ids=lb.GlobalKeys(global_keys), #Provide a list of global keys\n",
        "                                     task_queue_id=task_queues[2].uid #Passing None moves data rows to \"Done\" task queue\n",
        "                                    )"
      ],
      "cell_type": "code",
      "outputs": [],
      "execution_count": null
    },
    {
      "metadata": {},
      "source": [
        "### Fetch project configuration"
      ],
      "cell_type": "markdown"
    },
    {
      "metadata": {},
      "source": [
        "# Note the project is not fully setup many of the fields will be empty. \n",
        "print(\"Project is not setup yet:\", project.setup_complete is None)\n",
        "print(\"Project name:\", project.name)\n",
        "print(\"Project description:\", project.description)\n",
        "print(\"Media Type:\", project.media_type)\n",
        "batches = [b for b in project.batches()]\n",
        "print(\"Project Batches\", batches)\n",
        "print(\"Ontology:\", project.ontology())"
      ],
      "cell_type": "code",
      "outputs": [],
      "execution_count": null
    },
    {
      "metadata": {},
      "source": [
        "### Return number of labeled data rows"
      ],
      "cell_type": "markdown"
    },
    {
      "metadata": {},
      "source": [
        "print(\"Number of labels:\",  project.get_label_count())"
      ],
      "cell_type": "code",
      "outputs": [],
      "execution_count": null
    },
    {
      "metadata": {},
      "source": [
        "### Clean Up"
      ],
      "cell_type": "markdown"
    },
    {
      "metadata": {},
      "source": [
        "# project.delete()\n",
        "# dataset.delete()\n",
        "# client.delete_unused_ontology(ontology.uid)"
      ],
      "cell_type": "code",
      "outputs": [],
      "execution_count": null
    }
  ]
}